"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const blog_1 = require("../model/blog");
const fs = require("fs");
function default_1(app, direname) {
    app.get("/api/getTagBlog", function (req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const { blog_Tag } = req.query;
            const { success, data } = yield (0, blog_1.getTagBlog)(blog_Tag);
            // console.log(req.query);
            if (!success) {
                res.json({ success: false, msg: data.message });
                return;
            }
            res.json({ success: true, data: data });
        });
    });
    app.get("/api/getTypeBlog", function (req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const { blog_type } = req.query;
            const { success, data } = yield (0, blog_1.getTypeBlog)(blog_type);
            console.log(data);
            if (!success) {
                res.json({ success: false, msg: data.message });
                return;
            }
            res.json({ success: true, msg: data });
        });
    });
    //添加博客
    app.post("/api/addBlog", function (req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const blog = req.body;
            console.log(blog);
            const { success, data } = yield (0, blog_1.addblogData)(blog);
            console.log(data);
            if (!success) {
                res.json({ success: false, msg: "添加失败" });
                return;
            }
            res.json({ success: true, msg: "添加成功" });
        });
    });
    app.post("/api/getblogs", function (req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const { page } = req.body;
            let result = yield (0, blog_1.readBlogdata)(Number(page) || 1);
            if (!result.success) {
                res.json({ success: false, msg: result.data.message });
                return;
            }
            let resultTotal = yield (0, blog_1.getDataTotal)();
            res.json({
                success: true,
                data: { data: result.data },
                total: resultTotal.data[0].total,
            });
        });
    });
    // 删除博客的接口
    app.post("/api/delblog", function (req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const { id } = req.body;
            let { success, data } = yield (0, blog_1.delblog)(id);
            if (!success) {
                res.json({ success: false, msg: data.message });
            }
            res.json({ success: true, msg: "删除成功" });
        });
    });
    // 获取博客的详情
    app.get("/api/getDetail", function (req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const { id } = req.query;
            let { success, data } = yield (0, blog_1.getDetail)(id);
            if (!success) {
                res.json({ success: false, msg: data.message });
            }
            res.json({ success: true, data: data });
        });
    });
    // 更新博客
    app.post("/api/updateBlog", function (req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const blog = req.body;
            let { success, data } = yield (0, blog_1.updateBlog)(blog);
            if (!success) {
                res.json({ success: false, msg: data.message });
                return;
            }
            res.json({ success: true, msg: "更新成功" });
        });
    });
    // 文件上传接口
    // 文件上传接口
    app.post("/api/adddetailimg", function (req, res) {
        let name = req.files[0].originalname; // 获取上传文件名称
        let path = direname + "/static/upload/" + name; // 设置文件上传的路径
        fs.readFile(req.files[0].path, function (err, data) {
            if (err) {
                // 如果文件上传失败，显示错误消息到前端
                res.json({
                    success: "false",
                    message: "文件上传失败" + err.message,
                    errno: 1,
                });
                return;
            }
            // 把读取的图片流数据写入path路径
            fs.writeFile(path, data, function (err) {
                if (err) {
                    // 如果文件上传失败，显示错误消息到前端
                    res.json({
                        success: "false",
                        message: "文件上传失败" + err.message,
                        errno: 1,
                    });
                    return;
                }
                res.json({
                    success: true,
                    errno: 0,
                    data: {
                        url: "/static/upload/" + name,
                        alt: "",
                        href: "", // 图片的链接，非必须
                    },
                });
            });
        });
    });
    // 删除文件
    app.post("/api/deldetailimg", function (req, res) {
        const { file } = req.body;
        file.forEach(function (item) {
            // 拼接删除文件的路径
            item = direname + item;
            fs.unlink(item, function (err) {
                if (err) {
                    console.log(err);
                }
            });
        });
        res.json({ success: true, message: "删除成功" });
    });
}
exports.default = default_1;
